package hxy.bytecode.algorithm;

/**
 * 链表反转
 *
 * @author eric
 * @link https://www.jianshu.com/p/ce52c76eabf1
 */
public class LinkReverse {
    public static void main(String[] args) {
        LinkNode linkNode5 = new LinkNode(5, null);
        LinkNode linkNode4 = new LinkNode(4, linkNode5);
        LinkNode linkNode3 = new LinkNode(3, linkNode4);
        LinkNode linkNode2 = new LinkNode(2, linkNode3);
        LinkNode linkNode = new LinkNode(1, linkNode2);
        LinkNode reverse = reverse(linkNode);
        System.out.println(reverse);
    }

    /**
     * 关键点就是理解这个过程，并且用计算机语言实现
     * 三个指针
     *
     * @param current
     * @return
     */
    public static LinkNode reverse(LinkNode current) {
        LinkNode pre = null, next;
        while (current != null) {
            // 下面非常像冒泡排序的处理。首尾相连处理
            next = current.next;
            current.next = pre;
            pre = current;
            current = next;
        }
        return pre;
    }
}
